package com.cn.ysf.newbeemall.service.Impl;

import com.cn.ysf.newbeemall.common.Constants;
import com.cn.ysf.newbeemall.common.NewBeeMallCategoryLevelEnum;
import com.cn.ysf.newbeemall.controller.vo.NewBeeMallIndexCategoryVO;
import com.cn.ysf.newbeemall.controller.vo.SecondLevelCategoryVO;
import com.cn.ysf.newbeemall.controller.vo.ThirdLevelCategoryVO;
import com.cn.ysf.newbeemall.mapper.NewbeeGoodsCategoryMapper;
import com.cn.ysf.newbeemall.pojo.TbNewbeeMallGoodsCategory;
import com.cn.ysf.newbeemall.service.NewBeeMallCategoryService;
import com.cn.ysf.newbeemall.util.BeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.groupingBy;

/**
 * @author ysf
 * @date 2019/12/3 19:30
 */
@Service
public class NewBeeMallCategoryServiceImpl implements NewBeeMallCategoryService {


    @Resource
    private NewbeeGoodsCategoryMapper newbeeGoodsCategoryMapper;



    @Override
    public List<NewBeeMallIndexCategoryVO> getCategoriesForIndex() {

        List<NewBeeMallIndexCategoryVO> newBeeMallIndexCategoryVOSlist=new ArrayList<>();
        //获取一级商品分类的数据
        List<TbNewbeeMallGoodsCategory> firstcategories=newbeeGoodsCategoryMapper.selectByLevelAndParentIdsAndNumber(Collections.singletonList(0L), NewBeeMallCategoryLevelEnum.LEVEL_ONE.getLevel(), Constants.INDEX_CATEGORY_NUMBER);
        if(!CollectionUtils.isEmpty(firstcategories)){
            //获取一级商品分类的id
            List<Long> firstLevelCategoryIds=firstcategories.stream().map(TbNewbeeMallGoodsCategory::getCategoryId).collect(Collectors.toList());
            //获取二级分类的数据
            List<TbNewbeeMallGoodsCategory> secondcategories=newbeeGoodsCategoryMapper.selectByLevelAndParentIdsAndNumber(firstLevelCategoryIds,NewBeeMallCategoryLevelEnum.LEVEL_TWO.getLevel(),0L);
            if(!CollectionUtils.isEmpty(secondcategories)) {
                //获取二级商品分类的id
                List<Long> secondLevelCategoryIds = secondcategories.stream().map(TbNewbeeMallGoodsCategory::getCategoryId).collect(Collectors.toList());
                //获取三级商品分类数据
                List<TbNewbeeMallGoodsCategory> thirdcategories = newbeeGoodsCategoryMapper.selectByLevelAndParentIdsAndNumber(secondLevelCategoryIds, NewBeeMallCategoryLevelEnum.LEVEL_THREE.getLevel(), 0L);
                if (!CollectionUtils.isEmpty(thirdcategories)) {
                    //根据父级id将三级分类的数据进行分组
                    Map<Long, List<TbNewbeeMallGoodsCategory>> thirdLevelCategoryMap = thirdcategories.stream().collect(groupingBy(TbNewbeeMallGoodsCategory::getParentId));
                    List<SecondLevelCategoryVO> secondLevelCategoryVOSlist = new ArrayList<>();
                    //处理二级分类的数据
                    for (TbNewbeeMallGoodsCategory secondCategory : secondcategories) {
                        SecondLevelCategoryVO secondLevelCategoryVO = new SecondLevelCategoryVO();
                        BeanUtil.copyProperties(secondCategory, secondLevelCategoryVO);
                        //如果该二级分类下有数据则放入 secondLevelCategoryVO对象中
                        if (thirdLevelCategoryMap.containsKey(secondCategory.getCategoryId())) {
                            //根据二级分类的id取出thirdLevelCategoryMap分组中的三级分类list
                            List<TbNewbeeMallGoodsCategory> tempGoodsCategories = thirdLevelCategoryMap.get(secondCategory.getCategoryId());
                            secondLevelCategoryVO.setThirdLevelCategoryVOS((BeanUtil.copyList(tempGoodsCategories, ThirdLevelCategoryVO.class)));
                            secondLevelCategoryVOSlist.add(secondLevelCategoryVO);
                        }
                    }
                    //处理一级分类的数据
                    if (!CollectionUtils.isEmpty(secondLevelCategoryVOSlist)) {
                        //根据父级Id将thirdLevelCategories分组
                        Map<Long, List<SecondLevelCategoryVO>> secondlevelcategoryvomap = secondLevelCategoryVOSlist.stream().collect(groupingBy(SecondLevelCategoryVO::getParentId));
                        for (TbNewbeeMallGoodsCategory firstcategory : firstcategories) {
                            NewBeeMallIndexCategoryVO newBeeMallIndexCategoryVO = new NewBeeMallIndexCategoryVO();
                            BeanUtil.copyProperties(firstcategory, newBeeMallIndexCategoryVO);
                            //如果该一级分类下有数据则放入
                            if (secondlevelcategoryvomap.containsKey(firstcategory.getCategoryId())) {
                                //根据一级分类的id取出secondlevelcategoryvomap分组中的二级分类list
                                List<SecondLevelCategoryVO> tempGoodsCategories = secondlevelcategoryvomap.get(firstcategory.getCategoryId());
                                newBeeMallIndexCategoryVO.setSecondLevelCategoryVOS(tempGoodsCategories);
                                newBeeMallIndexCategoryVOSlist.add(newBeeMallIndexCategoryVO);
                            }
                        }
                    }
                }
            }
                return newBeeMallIndexCategoryVOSlist;
            }else {
                return null;
            }
        }

    /**
     * 根据parentId和level获取分类列表
     * @param parentIds
     * @param categoryLevel
     * @return
     */
    @Override
    public List<TbNewbeeMallGoodsCategory> selectBylevelAndParentAndNumber(List<Long> parentIds, Long categoryLevel) {
        return newbeeGoodsCategoryMapper.selectByLevelAndParentIdsAndNumber(parentIds,categoryLevel,0L);
    }

    /**
     * 根据商品分类的主键Id查询分类商品
     * @param id
     * @return
     */
    @Override
    public TbNewbeeMallGoodsCategory getGoodsCategoryById(Long id) {
        return newbeeGoodsCategoryMapper.selectByPrimaryKey(id);
    }
}
